from datetime import datetime
from django.db import models
from django.contrib.auth.models import AbstractUser


GENDER_CHOICES =(
    ("male","男"),
    ("female","女")
)

class BaseModel(models.Model):
    # now后不能加括号，否则会在系统编译的时候就调用这个函数，导致时间和实际不符
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        abstract = True

class UserProfile(AbstractUser):
    nick_name = models.CharField(max_length=50, verbose_name='昵称',default='')
    birthday = models.DateField(verbose_name='生日', null=True,blank=True)
    gender = models.CharField(verbose_name='性别', choices=GENDER_CHOICES,max_length=6)
    address = models.CharField(max_length=100, verbose_name='地址',default="")
    mobile = models.CharField(max_length=11, verbose_name="手机号码")      #unique为true则不允许重复数据
    image = models.ImageField(upload_to="head_image/%Y/%m", default="default.jpg",verbose_name='用户头像', null=True, blank=True)     #设置子路径,按/%Y年,%m月保存

    class Meta:
        verbose_name = "用户信息"
        verbose_name_plural = verbose_name

    def unread_nums(self):
        return self.usermessage_set.filter(has_read=False).count()

    def __str__(self):
        if self.nick_name:
            return self.nick_name
        else:
            return self.username


class EmailVerifyRecord(models.Model):
    """
    图形验证码
    """
    send_choices = (
        ('register','注册'),
        ('forget','找回密码')
    )

    code = models.CharField('验证码',max_length=20)
    email = models.EmailField('邮箱',max_length=50)
    send_type = models.CharField(choices=send_choices,max_length=10)
    send_time = models.DateTimeField(default=datetime.now)
    active = models.BooleanField(default=True)

    class Meta:
        verbose_name = '邮箱验证码'
        verbose_name_plural = verbose_name
# Create your models here.
